// pages/goodsFabu/putongFabu/putongFabu.js
// 日期
const app = getApp();
const date = new Date()
const years = []
const months = []
const days = []
const hours = []
const minutes = []
const seconds = []
for (let i = 1990; i <= date.getFullYear() + 1; i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}

for (let i = 0; i <= 23; i++) {
  hours.push(i)
}
for (let i = 0; i <= 59; i++) {
  minutes.push(i)
  seconds.push(i)
}
Page({
  data: {
    sysScroll:"",
    scrollTop:'',
    sepnum:'',

    zanshiName: '', //暂时
    anshiId: '', //暂时
    showAnceng1: false, //暗层
    shopfenlModal: false, //店铺分类
    yijiModel: false, //一级分类
    erjiModel: false, //二级分类
    danweiModel: false, //单位
    addshopfenlModal: false,
    headIndex: 0,
    years: years, //日期初始化
    year: '',
    year1: '',
    months: months,
    month: '',
    month1: '',
    days: days,
    day: '',
    day1: '',
    hours: hours,
    hour: '',
    hour1: '',
    minutes: minutes,
    minute: '',
    minute1: '',
    seconds: seconds,
    second: '',
    second1: '',
    valueRiqi: '',
    valueShijian: '',
    showAnceng: false, //暗层
    goodsModalone: false, //商品开始时间年月日弹窗
    goodsModaltwo: false, //商品开始时间时分秒弹窗
    zanshistartTime: '', //点击下一步时暂时年月日时间
    goodsstartTimes: '', //选择后的开始时间订单日期
    startTimestr: '', //开始时间订单日期字符串
    goodssendTimes: '', //选择后的结束时间订单日期
    endTimestr: '', //结束时间订单日期字符串
    startorend: true, //ture是开始时间，flase结束时间

    region: [], //市区选择器
    array: [], //商品单位
    shuxingFenleiarray: [], //商品属性分类列表
    yijiFenlei: '', //一级分类ID
    erjiFenlei: '', //二级分类ID
    danweis: '', //单位ID
    shuxing: '', //属性ID
    index: '', //商品单位下标
    sumfenlei: '', //一级二级总分类
    yijiindex: '', //一级分类下标
    erjiindex: '', //二级分类下标
    shuxingindex: '', //属性分类下标
    goodsValName: '', //商品名称
    qibul: '', //起步量
    xiangoul:'', //限购量
    qibuyunf: '', //起步运费
    xujianyunf: '', //续件运费
    imgURL: '', //信息封面图

    guigelist: [], //规格列表

    imglistobg: '', //上传的单个商品主图
    goodsZhutu: [], //商品主图列表
    xiangQingimg: '',

    hiddenmodalput: true, //新建商品属性显示
    name: "", //新添加的分类
    provinceId: '', //省ID
    cityId: '', //市ID
    townId: '', //区县ID,
    goodsId: '', //修改必填
    actId: '', //修改必填
  },
  onfocus(){
    this.setData({sysScroll: false})
  },
  onblur(){
    this.setData({sysScroll: true})
  },
  // 去选择区域
  toaddquyu() {
    wx.navigateTo({
      url: '/pages/goods/page/addtownid/addtownid?townList=' + JSON.stringify(this.data.region)
      // ?index=0&guigelist=' + JSON.stringify(this.data.guigelist),
    })
  },
  close_dingdanTime() {
    let that = this
    that.setData({
      showAnceng: false, //暗层
      goodsModalone: false, //商品开始时间年月日弹窗
      goodsModaltwo: false, //商品开始时间时分秒弹窗
    })
  },
  // 日期切换
  riqiChange: function (e) {
    let that = this
    const val = e.detail.value
    if (that.data.startorend) {
      this.setData({
        year: this.data.years[val[0]],
        month: this.data.months[val[1]],
        day: this.data.days[val[2]]
      })
    } else {
      this.setData({
        year1: this.data.years[val[0]],
        month1: this.data.months[val[1]],
        day1: this.data.days[val[2]]
      })
    }

  },
  // 时间切换
  shijianChange: function (e) {
    const val = e.detail.value
    let that = this
    if (that.data.startorend) {
      this.setData({
        hour: this.data.hours[val[0]],
        minute: this.data.minutes[val[1]],
        // second: this.data.seconds[val[2]]
      })
    } else {
      this.setData({
        hour1: this.data.hours[val[0]],
        minute1: this.data.minutes[val[1]],
        // second1: this.data.seconds[val[2]]
      })
    }

  },
  // 统计开始时间
  tongJiStart(e) {
    let that = this
    let dates = new Date()
    that.setData({
      year: dates.getFullYear(),
      year1: dates.getFullYear(),
      month: dates.getMonth() + 1,
      month1: dates.getMonth() + 1,
      day: dates.getDate(),
      day1: dates.getDate(),
      hour: dates.getHours(),
      hour1: dates.getHours(),
      minute: dates.getMinutes(),
      minute1: dates.getMinutes(),
      second: '00',
      second1: '00',
      valueRiqi: [dates.getFullYear() - 1990, dates.getMonth(), dates.getDate() - 1],
      valueShijian: [dates.getHours(), dates.getMinutes(), '00'],
    })


    console.log(e.currentTarget.dataset.startorend)
    let startorend
    let valueRiqi, valueShijian
    if (e.currentTarget.dataset.startorend == 1) {
      startorend = true
      valueRiqi = [(that.data.year - 1990), that.data.month - 1, that.data.day - 1]
      valueShijian = [that.data.hour, that.data.minute, that.data.second]
    } else {
      startorend = false
      valueRiqi = [(that.data.year1 - 1990), that.data.month1 - 1, that.data.day1 - 1]
      valueShijian = [that.data.hour1, that.data.minute1, that.data.second1]
    }
    this.setData({
      goodsModalone: true,
      showAnceng: true,
      startorend: startorend,
      valueRiqi: valueRiqi,
      valueShijian: valueShijian,
      // year: e.currentTarget.dataset.year * 1,
      // month: e.currentTarget.dataset.month * 1,
    })
  },
  // 统计下一步时间
  xiayiBu: function () {
    console.log('开始')
    let that = this
    let times = {}
    if (that.data.startorend) {
      times.year = that.data.year
      if (that.data.month * 1 < 10) {
        times.month = '0' + that.data.month
      } else {
        times.month = that.data.month
      }
      if (that.data.day * 1 < 10) {
        times.day = '0' + that.data.day
      } else {
        times.day = that.data.day
      }
    } else {
      times.year = that.data.year1
      if (that.data.month1 * 1 < 10) {
        times.month = '0' + that.data.month1
      } else {
        times.month = that.data.month1
      }
      if (that.data.day1 * 1 < 10) {
        times.day = '0' + that.data.day1
      } else {
        times.day = that.data.day1
      }
    }
    this.setData({
      goodsModalone: false,
      goodsModaltwo: true,
      zanshistartTime: times
    })
  },
  // 统计确定时间
  quedingEnd: function () {
    let that = this
    let times = that.data.zanshistartTime
    if (that.data.startorend) {
      if (that.data.hour * 1 < 10) {
        times.hour = '0' + that.data.hour
      } else {
        times.hour = that.data.hour
      }
      if (that.data.minute * 1 < 10) {
        times.minute = '0' + that.data.minute
      } else {
        times.minute = that.data.minute
      }
      // if (that.data.second * 1 < 10) {
      //   times.second = '0' + that.data.second
      // } else {
      //   times.second = that.data.second
      // }
    } else {
      if (that.data.hour1 * 1 < 10) {
        times.hour = '0' + that.data.hour1
      } else {
        times.hour = that.data.hour1
      }
      if (that.data.minute1 * 1 < 10) {
        times.minute = '0' + that.data.minute1
      } else {
        times.minute = that.data.minute1
      }
      // if (that.data.second1 * 1 < 10) {
      //   times.second = '0' + that.data.second1
      // } else {
      //   times.second = that.data.second1
      // }
    }

    let timestr = times.year + '-' + times.month + '-' + times.day + ' ' + times.hour + ':' + times.minute + ':00'
    if (that.data.startorend) {
      this.setData({
        showAnceng: false,
        goodsModaltwo: false,
        goodsstartTimes: times,
        startTimestr: timestr
      })
    } else {
      this.setData({
        showAnceng: false,
        goodsModaltwo: false,
        goodssendTimes: times,
        endTimestr: timestr
      })
    }
  },

  // 添加规格
  addGuige() {
    wx.navigateTo({
      url: '/pages/goods/page/tianjiaGuige/putongGuige/putongGuige?index=1&guigelist=' + JSON.stringify(this.data.guigelist),
    })
  },
  // 修改规格
  toxiugai(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/goods/page/tianjiaGuige/putongGuige/putongGuige?index=1&guigelist=' + JSON.stringify(this.data.guigelist) + '&listitem=' + e.currentTarget.dataset.item,
    })
  },


  //起步量
  qibuliang(e) {
    this.setData({
      qibul: e.detail.value,
      sysScroll: true
    })
  },
  //限购量
  xiangouliang(e) {
    this.setData({
      xiangoul: e.detail.value,
      sysScroll: true
    })
  },
  //起步运费
  qibuyunfei(e) {
    this.setData({
      qibuyunf: e.detail.value,
      sysScroll: true
    })
  },
  qibuyunfeiShijiao(e) {
    let that = this
    // let listindex = e.currentTarget.dataset.index
    let values = e.detail.value
    if (values.indexOf('.') != -1) {
      values = (values * 1).toFixed(2)
    }
    if (values == 'NaN') {
      wx.showToast({
        title: '请输入正确的起步运费',
        icon: 'none'
      })
      values = ''
    }
    this.setData({
      qibuyunf: values,
      sysScroll: true
    })
  },
  //续件运费
  xujianyunfei(e) {
    this.setData({
      xujianyunf: e.detail.value,
      sysScroll: true
    })
  },
  xujianyfShijiao(e) {
    let that = this
    let values = e.detail.value
    if (values.indexOf('.') != -1) {
      values = (values * 1).toFixed(2)
    }
    if (values == 'NaN') {
      wx.showToast({
        title: '请输入正确的续件运费',
        icon: 'none'
      })
      values = ''
    }
    that.setData({
      xujianyunf: values,
      sysScroll: true
    })
  },
  // 获取商品一级二级分类
  getgoodstypelist() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getPlatGoodType"
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success(res) {
        // setTimeout(function () {
        //   wx.hideLoading()
        // }, 0)
        // setTimeout(function () {
        //   wx.showToast({
        //     title: res.data.resultNote,
        //     icon: 'none',
        //     duration: 1500
        //   })
        // }, 0)
        console.log(res, 12)
        that.setData({
          sumfenlei: res.data.pfgType
        })
        if (that.data.goodsId) {
          setTimeout(function () {
            wx.showLoading({
              title: '加载中',
              mask: true
            })
            that.getgoodsinfo()
          }, 500)
        }
        // if (res.data.result == 0) {
        //   setTimeout(function () {
        //     wx.navigateBack({
        //       delta: 1
        //     })
        //   }, 1500)
        // }
      }
    });
  },
  // 获取平台店铺分类
  getshoptypelist() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getGoodsTypes",
        shopId: wx.getStorageSync('shopId'),
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success(res) {
        console.log(res, 55)
        if (res.data.result == 0) {
          that.setData({
            shuxingFenleiarray: res.data.goodsTypes
          })
        }
      }
    });
  },
  // 获取商品的计量单位 
  getdanwei() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getGoodUnitData"
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        // setTimeout(function () {
        //   wx.hideLoading()
        // }, 0)
        // setTimeout(function () {
        //   wx.showToast({
        //     title: res.data.resultNote,
        //     icon: 'none',
        //     duration: 1500
        //   })
        // }, 0)
        console.log(res)
        that.setData({
          array: res.data.unitList
        })
        // if (res.data.result == 0) {
        //   setTimeout(function () {
        //     wx.navigateBack({
        //       delta: 1
        //     })
        //   }, 1500)
        // }
      }
    });
  },
  // 上传商品主图
  addzhutuImg() {
    let that = this
    that.setData({
      imglistobg: ''
    })
    wx.navigateTo({
      url: '/pages/setting/page/cropper/cropper?type=zhu',
    })
  },
  // 删除商品主图
  shanchuzhutu(e) {
    let indexs = e.currentTarget.dataset.index
    let tupianList = this.data.goodsZhutu
    tupianList.splice(indexs, 1)
    this.setData({
      goodsZhutu: tupianList
    })
  },
  // 删除商品规格
  shanchuguige(e) {
    let indexs = e.currentTarget.dataset.index
    let tupianList = this.data.guigelist
    tupianList.splice(indexs, 1)
    this.setData({
      guigelist: tupianList
    })
  },
  xiangqTu() {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths[0]
        if (res.tempFiles[0].size > 1000000) {
          wx.showToast({
            title: '图片大于1M，请重新选择！',
            icon: 'none'
          })
          return
        } else {
          setTimeout(function () {
            wx.showLoading({
              title: '上传中',
            })
          }, 0)
          wx.request({
            url: app.globalData.myurl,
            method: 'POST',
            data: {
              cmd: "uploadImg",
              imgFile: wx.getFileSystemManager().readFileSync(tempFilePaths, "base64"),
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success(res) {
              if (res.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (res) {
                      if (res.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              that.setData({
                xiangQingimg: res.data.imgUrl
              })
              setTimeout(function () {
                wx.hideLoading()
              }, 0)
              setTimeout(function () {
                wx.showToast({
                  title: res.data.resultNote,
                  icon: 'none',
                  duration: 1500
                })
              }, 0)
            }
          });
        }

      }
    })
  },
  // 添加商品封面图：
  addFengmian() {
    let that = this
    wx.navigateTo({
      url: '/pages/setting/page/cropper/cropper',
    })
  },
  // 重置信息
  chongzhiInfoo() {
    // console.log('重置信息')
    let that = this
    wx.showModal({
      title: '是否重置信息',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            region: [], //市区选择器
            index: '', //商品单位下标
            yijiFenlei: '',
            erjiFenlei: '',
            danweis: '',
            shuxing: '',
            index: '',
            yijiindex: '', //一级分类下标
            erjiindex: '', //二级分类下标
            shuxingindex: '', //属性分类下标
            goodsValName: '', //商品名称
            qibul: '',
            xiangoul:'',
            qibuyunf: '',
            xujianyunf: '', //续件运费
            imgURL: '', //商品封面图
            guigelist: [],
            imglistobg: '',
            goodsZhutu: [],
            xiangQingimg: '',
            name: '',
            startTimestr: '',
            endTimestr: ''
          })
        } else {
          console.log('点击取消回调')
        }
      }
    })

  },
  // 确认提交
  tiJiaoInfo() {
    let that = this
    if (!that.data.goodsValName) {
      wx.showToast({
        title: '请输入商品名称',
        icon: 'none'
      })
      return
    }
    if (!that.data.shuxing) {
      wx.showToast({
        title: '请选择店铺商品属性分类',
        icon: 'none'
      })
      return
    }
    if (!that.data.yijiFenlei) {
      wx.showToast({
        title: '请选择一级分类',
        icon: 'none'
      })
      return
    }
    if (!that.data.erjiFenlei) {
      wx.showToast({
        title: '请选择二级分类',
        icon: 'none'
      })
      return
    }
    if (!that.data.danweis) {
      wx.showToast({
        title: '请选择商品单位',
        icon: 'none'
      })
      return
    }
    if (!that.data.imgURL) {
      wx.showToast({
        title: '请上传封面图',
        icon: 'none'
      })
      return
    }
    if (that.data.region.length < 1) {
      wx.showToast({
        title: '请选择销售区域',
        icon: 'none'
      })
      return
    }
    let tlist = []
    let list = that.data.region
    for (var index in list) {
      tlist.push(list[index].townId)
    }

    if (!that.data.qibul) {
      wx.showToast({
        title: '请输入起步量',
        icon: 'none'
      })
      return
    }
    let qibuyunf = that.data.qibuyunf
    if (!that.data.qibuyunf) {
      // wx.showToast({
      //   title: '请输入起步运费',
      //   icon: 'none'
      // })
      // return
      qibuyunf = 0
    }
    let xujianyunf = that.data.xujianyunf
    if (!that.data.xujianyunf) {
      // wx.showToast({
      //   title: '请输入续件运费',
      //   icon: 'none'
      // })
      // return
      xujianyunf = 0
    }
    if (!that.data.startTimestr) {
      wx.showToast({
        title: '请选择活动开始时间',
        icon: 'none'
      })
      return
    }
    if (!that.data.endTimestr) {
      wx.showToast({
        title: '请选择活动结束时间',
        icon: 'none'
      })
      return
    }
    if (that.data.goodsZhutu.length < 1) {
      wx.showToast({
        title: '请上传商品主图',
        icon: 'none'
      })
      return
    }
    if (!that.data.xiangQingimg) {
      wx.showToast({
        title: '请上传商品详情图',
        icon: 'none'
      })
      return
    }
    if (that.data.guigelist.length < 1) {
      wx.showToast({
        title: '请至少添加一种规格',
        icon: 'none'
      })
      return
    }
    let actBean = {
      actId: that.data.actId,
      startTime: that.data.startTimestr,
      endTtime: that.data.endTimestr,
    }
    let type
    if (that.data.goodsId) {
      type = 2
    } else {
      type = 1
    }
    wx.showModal({
      title: '提示',
      content: '是否确认提交？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          
          wx.request({
            url: app.globalData.myurl,
            data: {
              cmd: "editLimitedGoods",
              shopId: wx.getStorageSync('shopId'),
              userId: wx.getStorageSync('userId'),
              type: type,
              goodsId: that.data.goodsId,
              goodsName: that.data.goodsValName,
              goodsCoverImg: that.data.imgURL,
              goodsTypeId: that.data.shuxing,
              pfgtypeId2: that.data.erjiFenlei,
              unitNo: that.data.danweis,
              townId: JSON.stringify(tlist),
              goodsDesc: that.data.xiangQingimg,
              startFreight: qibuyunf,
              incFreight: xujianyunf,
              skuBean: JSON.stringify(that.data.guigelist),
              goodsImg: JSON.stringify(that.data.goodsZhutu),
              minbuynum: that.data.qibul,
              maxbuynum: that.data.xiangoul,
              actBean: JSON.stringify(actBean)
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success:function(ress) {
              if (ress.data.result == 0) {
                wx.showToast({
                  title: ress.data.resultNote,
                  icon: 'none',
                  duration: 1500
                })
                // wx.hideLoading()
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 1500)
                return false;
              } else {
                wx.showToast({
                  title: ress.data.resultNote,
                  icon: 'none',
                  duration: 1500
                })
              }
              if (ress.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (resss) {
                      if (resss.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              
            }
          });

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  // 商品名称
  goodsVal(e) {
    console.log(e);
    if(e.detail.cursor == 40){
      wx.showToast({
        title: '最多输入40个字符哦',
        icon:"none",
        duration:1500
      })
    }
    this.setData({
      goodsValName: e.detail.value.replace(/[\r\n]/g,"")
    })
  },
  // 头部切换
  changeIndex(e) {
    let that = this
    console.log(e.currentTarget.dataset.index)
    that.setData({
      headIndex: e.currentTarget.dataset.index
    })
  },










  // 关闭弹窗*******************************************
  close_dingdanTime1: function () {
    this.setData({
      shopfenlModal: false, //店铺分类
      showAnceng1: false, //暗层的显示
      yijiModel: false,
      erjiModel: false,
      danweiModel: false,
      addshopfenlModal: false
    })
  },
  // 点击店普分类弹窗*****************************************
  showshoplist() {
    let that = this
    if (!that.data.shuxingFenleiarray.length) {
      that.setData({
        showAnceng1: true,
        shopfenlModal: true,
        zanshiName: '',
        anshiId: ''
      })
    } else {
      that.setData({
        showAnceng1: true,
        shopfenlModal: true,
        zanshiName: 0,
        anshiId: that.data.shuxingFenleiarray[0].goodsTypeId
      })
    }

  },
  // 店铺暂时存储
  changeshuxingVal(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this
    that.setData({
      zanshiName: e.detail.value[0],
      anshiId: that.data.shuxingFenleiarray[e.detail.value[0]].goodsTypeId
    })
  },
  // 确定店铺属性分类
  shuxingChange: function () {
    let that = this
    that.setData({
      shuxingindex: that.data.zanshiName,
      shuxing: that.data.anshiId,
      showAnceng1: false,
      shopfenlModal: false,
    })
  },
  // 新增分类***********************
  addshuxingss() {
    let that = this
    that.setData({
      showAnceng1: true,
      addshopfenlModal: true,
      shopfenlModal: false,
    })
  },
  // 添加新店铺分类
  iName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  inum: function (e) {
    this.setData({
      sepnum: e.detail.value
    })
  },
  // 添加新店铺分类
  confirmM: function (e) {
    let that = this
    if (!that.data.name) {
      wx.showToast({
        title: '请输入分类',
        icon: 'none'
      })
      return
    }
    if (!that.data.sepnum) {
      wx.showToast({
        title: '请输入分类序号',
        icon: 'none'
      })
      return
    }
    setTimeout(function () {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "upGoodsTypes",
        shopId: wx.getStorageSync('shopId'),
        name: that.data.name,
        seq: that.data.sepnum,
        state: 0,
        type: 1
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          setTimeout(function () {
            wx.showToast({
              title: '添加成功',
              icon: 'none',
              duration: 1500
            })
          }, 0)
          that.getshoptypelist()
          that.setData({
            addshopfenlModal: false,
            showAnceng1: false,
          })
        }
      }
    });
  },

  // 点击一级分类弹窗******************************************
  showyijilist() {
    let that = this
    that.setData({
      showAnceng1: true,
      yijiModel: true,
      zanshiName: 0,
      anshiId: that.data.sumfenlei[0].pfgTypeId1
    })
  },
  // 一级暂时存储
  changeyijiVal(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this
    that.setData({
      zanshiName: e.detail.value[0],
      anshiId: that.data.sumfenlei[e.detail.value[0]].pfgTypeId1
    })
  },
  // 确定1级分类
  yijiChange: function () {
    let that = this
    this.setData({
      yijiindex: that.data.zanshiName,
      yijiFenlei: that.data.anshiId,
      showAnceng1: false, //暗层的显示
      yijiModel: false,
      erjiindex: '',
      erjiFenlei: ''
    })
  },
  // 未选一级，选二级******************************
  yesorno() {
    setTimeout(function () {
      wx.showToast({
        title: '请先选择一级分类',
        icon: 'none',
        duration: 1500
      })
    }, 0)
  },
  // 点击二级分类弹窗**********************************
  showerjilist() {
    let that = this
    that.setData({
      showAnceng1: true,
      erjiModel: true,
      zanshiName: 0,
      anshiId: that.data.sumfenlei[that.data.yijiindex].subTypeList[0].pfgTypeId2
    })
  },
  // 二级暂时存储
  changeerjiVal(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this
    that.setData({
      zanshiName: e.detail.value[0],
      anshiId: that.data.sumfenlei[that.data.yijiindex].subTypeList[e.detail.value[0]].pfgTypeId2
    })
  },
  // 确定二级分类
  erjiChange: function (e) {
    let that = this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      erjiindex: that.data.zanshiName,
      erjiFenlei: that.data.anshiId,
      showAnceng1: false,
      erjiModel: false,
    })
  },

  // 点击单位弹窗******************************************************************
  showdanweilist() {
    let that = this
    that.setData({
      showAnceng1: true,
      danweiModel: true,
      zanshiName: 0,
      anshiId: that.data.array[0].unitNo
    })
  },
  // 单位暂时存储
  changedanweiVal(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this
    that.setData({
      zanshiName: e.detail.value[0],
      anshiId: that.data.array[e.detail.value[0]].unitNo
    })
  },
  //确定单位
  danweiChange: function (e) {
    let that = this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      index: that.data.zanshiName,
      danweis: that.data.anshiId,
      showAnceng1: false,
      danweiModel: false,
    })
  },


































  getgoodsinfo() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "limitedGoodsDetails",
        shopId: wx.getStorageSync('shopId'),
        goodsId: that.data.goodsId
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
      },
      success(res) {
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          let danweiindex //单位
          for (var index1 in that.data.array) {
            if (that.data.array[index1].unit == res.data.goodsUnit) {
              danweiindex = that.data.array[index1].unitNo
            }
          }
          // 店铺属性分类
          let shuxiangfenleiindex = 0
          for (let i in that.data.shuxingFenleiarray) {
            if (that.data.shuxingFenleiarray[i].goodsTypeId == res.data.typeId) {
              shuxiangfenleiindex = i
            }
          }
          // 一级分类、二级分类
          let yijiindex = 0
          let erjiindex = 0
          for (let j in that.data.sumfenlei) {
            let obj = that.data.sumfenlei[j]
            if (obj.pfgTypeId1 == res.data.pfgId1) {
              yijiindex = j
              for (let k in obj.subTypeList) {
                if (obj.subTypeList[k].pfgTypeId2 == res.data.pfgId2) {
                  erjiindex = k
                }
              }
            }
          }
          // 销售区域
          let salesArea = res.data.salesArea
          let salesArealist = []
          for (let l in salesArea) {
            salesArealist.push({
              town: salesArea[l],
              townId: l,
              actives: true
            })
          }
          that.setData({
            goodsValName: res.data.goodsName,
            danweis: danweiindex,
            index: danweiindex,
            imgURL: res.data.goodsCoverImg,
            qibuyunf: res.data.startFreight,
            xujianyunf: res.data.incFreight,
            actId: res.data.actBean.actId,
            guigelist: res.data.goodSkuList,
            goodsZhutu: res.data.goodsImg,
            xiangQingimg: res.data.goodsDesc,


            startTimestr: res.data.actBean.startTime,
            endTimestr: res.data.actBean.endTtime,



            shuxingindex: shuxiangfenleiindex,
            shuxing: that.data.shuxingFenleiarray[shuxiangfenleiindex].goodsTypeId, //属性ID
            yijiindex: yijiindex,
            yijiFenlei: that.data.sumfenlei[yijiindex].pfgTypeId1,
            erjiindex: erjiindex,
            erjiFenlei: that.data.sumfenlei[yijiindex].subTypeList[erjiindex].pfgTypeId2,
            region: salesArealist,
            qibul: res.data.minbuynum,
            xiangoul:res.data.maxbuynum
          })
          // }
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    if (options.goodsId) {
      that.setData({
        goodsId: options.goodsId
      })
    }
    //console.log(that.data.region)
    that.getgoodstypelist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    let goodsZhutu = that.data.goodsZhutu
    if (that.data.imglistobg) {
      goodsZhutu.push(that.data.imglistobg)
    }
    that.setData({
      goodsZhutu: goodsZhutu
    })
    that.getdanwei()
    that.getshoptypelist()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let that = this
    that.setData({
      imglistobg: ''
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    that.setData({
      imglistobg: ''
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '众商联采',
      path: "/pages/login/login", // 路径，传递参数到指定页面。
      // imageUrl:'../../../images/logo/LOGO.png'
      imageUrl: app.globalData.logo
    }
  }
})